// @algorithm @lc id=100324 lang=cpp
// @title he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof

#include "algm/algm.h"
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
class Solution {
    // @test(10)=[[1,2,3,4]]
public:
    vector<vector<int>> findContinuousSequence(int target) {
        vector<vector<int>> ans;
        vector<int> temp;
        for (int i = 1; i <= target / 2 + 1; ++i) {
            if (oddHelper(target, i, temp)) {
                ans.push_back(temp);
            }
            if (evenHelper(target, i, temp)) {
                ans.push_back(temp);
            }
        }
        return ans;
    }

    bool oddHelper(int target, int mid, vector<int> &temp) {
        // 奇数项，等差中项为mid
        if (target % mid != 0) {
            return false;
        }
        // 项数为target/mid
        int cnt = target / mid;
        if (cnt % 2 == 0) {
            return false;
        }
        if (cnt / 2 >= mid) {
            return false;
        }
        temp.resize(cnt);
        iota(temp.begin(), temp.end(), mid - cnt / 2);
        return true;
    }

    bool evenHelper(int target, int mid, vector<int> &temp) {
        // 偶数项，等差中项为mid+0.5
        if (target % (mid + mid + 1) != 0) {
            return false;
        }
        // 项数为cnt
        int cnt = target / (mid + mid + 1) * 2;
        if (cnt % 2 != 0) {
            return false;
        }
        if (cnt / 2 > mid) {
            return false;
        }
        temp.resize(cnt);
        iota(temp.begin(), temp.end(), mid + 1 - cnt / 2);
        return true;
    }
};